package klex.eserver.action.systemmanager.permission;

import java.util.Date;
import java.util.List;

import klex.eserver.action.BaseAction;
import klex.eserver.entity.OnlineClientFunctions;
import klex.eserver.entity.OnlineClientPermissions;
import klex.eserver.javabean.AjaxDone;
import klex.eserver.javabean.FunctionBean;

/**
 * ְ职责action
 * @author 
 *
 */
public class FunctionAction extends BaseAction{
	
	private List<OnlineClientFunctions> functions;			//职责集合
	private OnlineClientFunctions clientFunctions;			//ְ职责对象
	private List<FunctionBean> functionBeans;				//ְ自定义bean集合
	private FunctionBean functionBean;						//ְ自定义bean对象
	private String[] args;									//
	private String functionId;								//职责id
	private AjaxDone ajaxDone =new AjaxDone();
	private String str1="",str2="",str3 = "",str4="",tem="",tem1="",tem2="",strtem="";   // 级别存储变量
	private int i=1;										// 表示级别数
	private List<OnlineClientPermissions> permissions;	 	//功能集合
	private String perID;									//功能id字符串
	private String pID="";
	/**
	 * 查询全部职责信息
	 * @return
	 */
	public String findAllFunction(){
		functionBeans  = functionBiz.findAllFunctions();
		request.put("functionBeans", functionBeans);
		return SUCCESS;
	}
	
	/**
	 * 组合查询全部职责信息
	 * @return
	 */
	public String findFunction(){
		functionBeans = functionBiz.findFunctionsByName(clientFunctions);
		request.put("functionBeans", functionBeans);
		return SUCCESS;
	}
	/**
	 * 新增职责信息
	 * @return
	 */
	public String addFunction(){
		if (null !=clientFunctions.getFunctionsStaffPermissions()&& !"".equals(clientFunctions.getFunctionsStaffPermissions())) {
			String[] nodes = clientFunctions.getFunctionsStaffPermissions().split(",");	
			for (int i = 0; i < nodes.length; i++) {
				String[] nodeKeyVal = nodes[i].split(":");				
				pID +=nodeKeyVal[1]+",";
			}
		}
		clientFunctions.setFunctionsStaffPermissions(pID);
		clientFunctions.setFunctionsAddDate(new Date());		
		functionBiz.addFunctions(clientFunctions);
		
		ajaxDone.setNavTabId("findFunction");
		ajaxDone.setRel("findFunction");
		ajaxDone.setCallbackType(AjaxDone.CLOSECURRENT);
		request.put("ajaxDone", ajaxDone);
		return SUCCESS;
		}
	/**
	 * 根据职责id查询一条数据
	 * @return
	 */
	public String getFunctionByNumber(){
		clientFunctions =  functionBiz.getFunctionsByNumber(clientFunctions.getFunctionsNumber());
		if(null !=clientFunctions.getFunctionsStaffPermissions()){
			//分割功能id
			String str = clientFunctions.getFunctionsStaffPermissions();
			args = str.split(",");
			request.put("args", args);
		}
		request.put("clientFunctions", clientFunctions);		
		return SUCCESS;
	}
	/**
	 * 修改职责信息
	 * @return
	 */
	public String updateFunction(){
		functionBiz.updateFunctionsByNumber(clientFunctions);
		
		ajaxDone.setNavTabId("x_function");
		ajaxDone.setRel("x_function");
		ajaxDone.setCallbackType(ajaxDone.CLOSECURRENT);
		ajaxDone.setForwardUrl("findFunction.action");
		request.put("ajaxDone", ajaxDone);
		return SUCCESS;
	}
	
	/**
	 * 删除职责信息（批量删除）
	 * @return
	 */
	public String delFunction(){
		String sql = "delete kexekey.online_client_functions f where f.FUNCTIONS_NUMBER in("+functionId+")";
		functionBiz.delAllFunction(sql);	
		
		ajaxDone.setNavTabId("x_function");
		ajaxDone.setRel("x_function");
		ajaxDone.setCallbackType(ajaxDone.FORWARD);
		ajaxDone.setForwardUrl("findFunction.action");
		request.put("ajaxDone", ajaxDone);
		return SUCCESS;
	}
	
	/**
	 * 进入增加职责页面（查询全部功能数据）
	 * @return
	 */
	public String findAllPerByFun(){
		str1 = "";str2 = "";str3 = "";str4 = "";tem = "";tem1 = "";tem2 = "";strtem = "";i = 0;
		
		permissions = permissionBiz.findAllPermission();
		for (OnlineClientPermissions permission : permissions) {
			if (permission.getPermissionsClasstype().equals(1)) {
				tem1 = "<li><a checked='true' tvalue='" + permission.getPermissionsNumber()
						+ "' name = 'ids' " + "rel = '"+ permission.getPermissionsNumber() + "' " + "tname='"
						+ permission.getPermissionsNumber() + "' >"
						+ permission.getPermissionsName() + "  </a></li>";
				strtem += tem1;
				str1 = strtem;
			}
			if (permission.getPermissionsClasstype().equals(0)) {
				if (!permission.getPermissionsMenu().substring(0, 1)
						.equals("0")) {
					if (!permission.getPermissionsMenu().substring(2, 3)
							.equals("0")) {
						if (!permission.getPermissionsMenu().substring(4, 5)
								.equals("0")) {
							if (permission.getPermissionsMenu().substring(6, 7)
									.equals("0")) {
								// str4="<li><a>"+permission.getPermissionsName()+"</a><ul>strtem</ul></li>";
								// String string = new String(str4);
								// str4 = string.replaceAll("strtem",strtem);
								// strtem="";
							} else {
								// str4+=strtem;
							}
						} else {
							tem2 += str3;
							str3 = "<li><a checked='true' tvalue='"
									+ permission.getPermissionsNumber()
									+ "' name = 'ids'" + "rel = '"
									+ permission.getPermissionsNumber() + "' "
									+ "tname='"
									+ permission.getPermissionsNumber() + "' >"
									+ permission.getPermissionsName()
									+ " </a><ul>strtem</ul></li>";
							String string = new String(str3);
							str3 = string.replaceAll("strtem", strtem);
							strtem = "";
							i++;
						}
					} else {
						if (i == 1) {
							strtem = str3;
						} else {
							strtem = tem2 + str3;

						}
						str2 = "<li><a  checked='true' tvalue='"
								+ permission.getPermissionsNumber()
								+ "' name = 'ids'" + "rel = '"
								+ permission.getPermissionsNumber() + "' "
								+ "tname='" + permission.getPermissionsNumber()
								+ "' >" + permission.getPermissionsName()
								+ "  </a><ul>strtem</ul></li>";
						String string = new String(str2);
						str2 = string.replaceAll("strtem", strtem);
						tem = str2 + tem;
						strtem = "";
						tem2 = "";
						str3 = "";
					}
				} else {
					// str1="<li><a>"+permission.getPermissionsName()+"</a><ul>strtem</ul></li>";
					// strtem+=str2;
					// str1+=str2;
					// String string = new String(str1);
					// str1 = string.replaceAll("strtem",strtem);
					// str1+=strtem;
					// strtem="";
				}
			}
		}
		request.put("tem", tem);
		return SUCCESS;
	}
	
	
	
	/**
	 * get 和set
	 * @return
	 */
	public List<OnlineClientFunctions> getFunctions() {
		return functions;
	}

	public void setFunctions(List<OnlineClientFunctions> functions) {
		this.functions = functions;
	}

	public OnlineClientFunctions getClientFunctions() {
		return clientFunctions;
	}

	public void setClientFunctions(OnlineClientFunctions clientFunctions) {
		this.clientFunctions = clientFunctions;
	}

	public List<FunctionBean> getFunctionBeans() {
		return functionBeans;
	}

	public void setFunctionBeans(List<FunctionBean> functionBeans) {
		this.functionBeans = functionBeans;
	}

	public FunctionBean getFunctionBean() {
		return functionBean;
	}

	public void setFunctionBean(FunctionBean functionBean) {
		this.functionBean = functionBean;
	}

	public String[] getArgs() {
		return args;
	}

	public void setArgs(String[] args) {
		this.args = args;
	}

	public List<OnlineClientPermissions> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<OnlineClientPermissions> permissions) {
		this.permissions = permissions;
	}

	public String getTem() {
		return tem;
	}

	public void setTem(String tem) {
		this.tem = tem;
	}

	public AjaxDone getAjaxDone() {
		return ajaxDone;
	}

	public void setAjaxDone(AjaxDone ajaxDone) {
		this.ajaxDone = ajaxDone;
	}

	public String getStr1() {
		return str1;
	}

	public void setStr1(String str1) {
		this.str1 = str1;
	}

	public String getStr2() {
		return str2;
	}

	public void setStr2(String str2) {
		this.str2 = str2;
	}

	public String getStr3() {
		return str3;
	}

	public void setStr3(String str3) {
		this.str3 = str3;
	}

	public String getStr4() {
		return str4;
	}

	public void setStr4(String str4) {
		this.str4 = str4;
	}

	public String getTem1() {
		return tem1;
	}

	public void setTem1(String tem1) {
		this.tem1 = tem1;
	}

	public String getTem2() {
		return tem2;
	}

	public void setTem2(String tem2) {
		this.tem2 = tem2;
	}

	public String getStrtem() {
		return strtem;
	}

	public void setStrtem(String strtem) {
		this.strtem = strtem;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public String getFunctionId() {
		return functionId;
	}

	public void setFunctionId(String functionId) {
		this.functionId = functionId;
	}

	public String getPerID() {
		return perID;
	}

	public void setPerID(String perID) {
		this.perID = perID;
	}
}
